package com.ruoyi.iot.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.ruoyi.iot.domain.data.FieldRunData;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DianJHttpUtil {
    public static List<FieldRunData> getData(String url, String deviceCode,String token){
        String getUrl=url+deviceCode;
        HashMap<String,Object> kv=new HashMap<String, Object>();
        kv.put("accessToken",token);
        JSONObject lastData=HttpUtils.getUrlHttpDataUrlParam(getUrl,kv);
        String statueCode=lastData.getStr("code");
        List<FieldRunData> list=new ArrayList<FieldRunData>();
        if(statueCode.equals("01")){
            JSONObject result= lastData.getJSONObject("result");
            JSONArray dataList = result.getJSONArray("dataValues");
            for (JSONObject cData : dataList.toList(JSONObject.class)) {
                String key=cData.getStr("channelId");
                String value=cData.getStr("value");
                String monitorTime=cData.getStr("varDate");
                FieldRunData rData=new FieldRunData();
                rData.setField(key);
                rData.setFieldValue(value);
                rData.setMonitoringTime(LocalDateTime.parse(monitorTime, DatePattern.NORM_DATETIME_FORMATTER));
                list.add(rData);
            }
        }
        return list;
    }
}
